/**
 * 
 */
package tests;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import database.DBHandler;
import forum.Member;
import forum.UsersHandler;

/**
 * @author Alon Segal
 *
 */
public class TestUsersHandler {
	
	private UsersHandler uh;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		uh = new UsersHandler(new DBHandler());
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link forum.UsersHandler#registerUser(forum.Member, java.lang.String)}.
	 */
	@Test
	public void testRegisterUser() {
		uh.removeUser("testuser1");
		Member m = new Member("testuser1", "a", "b", "a@b.c", "m");
		assertTrue("user was not added properly", uh.registerUser(m, "123"));
	}

	/**
	 * Test method for {@link forum.UsersHandler#addUserFriend(java.lang.String, java.lang.String)}.
	 */
	@Test
	public void testAddUserFriend() {
		uh.addUserFriend("alonseg", "testuser1");
		List<Member> friends1 = uh.getListOfFriends("alonseg");
		List<Member> friends2 = uh.getListOfFriends("testuser1");
		boolean ans1 = false;
		for(int i=0; i<friends1.size(); i++) {
			if(friends1.get(i).getUsername().equals("testuser1")) {
				ans1 = true;
			}
		}
		boolean ans2 = false;
		for(int i=0; i<friends2.size(); i++) {
			if(friends2.get(i).getUsername().equals("alonseg")) {
				ans2 = true;
			}
		}
		assertTrue("friend was not added properly", ans1);
		assertTrue("friend was not added properly", ans2);
	}

	/**
	 * Test method for {@link forum.UsersHandler#removeUserFriend(java.lang.String, java.lang.String)}.
	 */
	@Test
	public void testRemoveUserFriend() {
		uh.removeUserFriend("alonseg", "testuser1");
		List<Member> friends1 = uh.getListOfFriends("alonseg");
		List<Member> friends2 = uh.getListOfFriends("testuser1");
		boolean ans1 = false;
		for(int i=0; i<friends1.size(); i++) {
			if(friends1.get(i).getUsername().equals("testuser1")) {
				ans1 = true;
			}
		}
		boolean ans2 = false;
		for(int i=0; i<friends2.size(); i++) {
			if(friends2.get(i).getUsername().equals("alonseg")) {
				ans2 = true;
			}
		}
		assertFalse("friend was not added properly", ans1);
		assertFalse("friend was not added properly", ans2);
	}

	/**
	 * Test method for {@link forum.UsersHandler#login(java.lang.String, java.lang.String)}.
	 */
	@Test
	public void testLogin() {
		assertTrue("User was not logged in properly", uh.login("testuser1", "123"));
	}

	/**
	 * Test method for {@link forum.UsersHandler#logoff(java.lang.String)}.
	 */
	@Test
	public void testUserLoggedOff() {
		uh.login("testuser1", "123");
		assertTrue("User was not logged out properly", uh.logoff("testuser1"));
	}

	/**
	 * Test method for {@link forum.UsersHandler#isModerator(java.lang.String)}.
	 */
	@Test
	public void testIsModerator() {
		assertFalse("suppose to be a moderrator", uh.isModerator("testuser1"));
	}

}
